<template>
  <div class="summary-dialog">
    <el-dialog
      title="意见汇总"
      :visible.sync="summaryShow"
      width="628px"
      :before-close="handleClose"
      :close-on-click-modal="false"
      custom-class="dialog-box"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="118px"
        class="demo-ruleForm"
      >
        <el-form-item label="汇总结果" prop="approvalStatus">
          <el-radio-group v-model="ruleForm.approvalStatus">
            <el-radio :label="14">通过</el-radio>
            <el-radio :label="15">退回重申</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="汇总意见" prop="approvalOpinion" class>
          <el-input
            type="textarea"
            v-model="ruleForm.approvalOpinion"
            maxlength="1000"
            show-word-limit
          ></el-input>
          <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
        </el-form-item>
        <div class="select-template">
          <el-dropdown trigger="click" @command="handleCommand" placement="top">
            <span class="el-dropdown-link">
              选择模版<i class="icon-arrow-down el-icon-folder-remove"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                icon="el-icon-circle-check"
                v-for="dict in selectDemoList"
                :key="dict.dictValue"
                :command="dict.dictValue"
                >{{ dict.dictLabel }}</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <el-form-item label="附件" prop="approvalDatabase.fileName">
          <el-upload
            ref="upload"
            class="upload-demo"
            :action="upload.url"
            accept=".png, .jpg, .pdf, .doc, .docx, .zip, .rar, .7z"
            :headers="upload.headers"
            :limit="1"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
          >
            <el-input
              v-model="ruleForm.approvalDatabase.fileName"
              placeholder="请选择文件"
            />
            <div class="img-box">
              <img
                src="@/assets/images/tripartite/icon_files_upload.png"
                alt=""
              />
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getApprovalAdd } from "@/api/manage/manage";
import { getToken } from "@/utils/auth";

export default {
  name: "summaryDialog",
  props: {
    summaryShow: {
      type: Boolean,
      default: "",
    },
    type: {
      type: String,
      default: "",
    },
    proid: {
      type: String,
      default: "",
    },
  },
  inject: ["reload"],
  data() {
    return {
      rules: {
        approvalStatus: [
          { required: true, message: "请选择结果", trigger: "change" },
        ],
        // approvalOpinion: [
        //   {
        //     required: true,
        //     message: "请输入原因",
        //     trigger: ["blur", "change"],
        //   },
        // ],
      },
      ruleForm: {
        approvalStatus: "",
        approvalOpinion: "",
        approvalDatabase: { fileName: "", fileUrl: "" },
      },
      // 选择模板数据字典
      selectDemoList: [
        { dictValue: "方案描述不充分1", dictLabel: "方案描述不充分1" },
        { dictValue: "方案描述不充分2", dictLabel: "方案描述不充分2" },
        { dictValue: "方案描述不充分3", dictLabel: "方案描述不充分3" },
      ],
      // 上传文件参数
      upload: {
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/file/upload",
        // 上传文件名称
        fileName: "请选择文件",
      },
    };
  },
  methods: {
    /** 取消按钮 */
    handleClose() {
      this.$refs.ruleForm.resetFields();
      this.$emit("changeShowDialog", "false"); //调用父组件的自定义事件，并传值
    },
    /** 确定按钮 */
    submitForm() {
      this.ruleForm.projectId = this.proid;
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          getApprovalAdd(this.ruleForm).then((res) => {
            console.log(res);
            this.$refs.ruleForm.resetFields();
            this.$emit("changeShowDialog", "false");
            this.reload();
          });
        }
      });
    },
    // 选择模板内容
    handleCommand(command) {
      this.ruleForm.approvalOpinion = command;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      console.log("文件上传中", file);
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      console.log("文件上传成功", response, file);
      this.$refs.upload.clearFiles();
      if(response.code!=500&&response.data){
        this.upload.fileName = file.name;
        this.ruleForm.approvalDatabase.fileName = file.name;
        this.ruleForm.approvalDatabase.fileUrl = response.data.fileUrl;
        this.$forceUpdate();
      }else{
        this.$message.error(response.msg)
      }
    },
  },
};
</script>

<style lang="scss">
.summary-dialog .el-form-item .el-input__inner {
  width: 100%;
}
.summary-dialog .el-form-item .el-textarea__inner {
  width: 100%;
}
.summary-dialog {
  // 下载模板样式修改
  .select-template {
    .el-dropdown-link {
      color: #3171ee;
    }
    .icon-arrow-down {
      margin-left: 10px;
      color: #3171ee;
    }
    // width: 100%;
    display: flex;
    align-items: center;
    justify-content: right;
    font-size: 14px;
    font-family: PingFang SC, PingFang SC-Regular;
    font-weight: 400;
    text-align: LEFT;
    color: #3171ee;
    margin-left: 30%;
    margin-bottom: 20px;
    cursor: pointer;
    img {
      margin-left: 10px;
    }
  }
  // 文件上传
  .upload-demo {
    .el-upload-list {
      display: none;
    }
    .el-upload {
      display: flex;
    }
    .img-box {
      height: 37px;
      width: 37px;
      line-height: 36px;
      background: #0d72ff;
      text-align: center;
      border-radius: 0px 4px 4px 0px;
      position: absolute;
      right: -1%;
      top: 1px;
      img {
        width: 16px;
        height: 16px;
      }
    }
  }
}
</style>
